package chapter6.meine.GUIexamples;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import chapter6.meine.GUIexamples.LabelWithSlidersAndBox.ContentPanel;

public class FourActionsCalc extends JApplet{
	

	 
	   public static void main(String[] args) {
		      JFrame window = new JFrame("Simple Calculator");
		      MainPanel panel = new MainPanel();
		      window.setContentPane(panel);
		      window.pack();  // Sizes window to preferred size of contents.
		      window.setLocation(100,100);
		      window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
		      window.setVisible(true);
		   }// end of main()
	   
	   
		 public void init() {
		      setContentPane( new MainPanel() );
		   }//end of init()
	   
	public static class MainPanel extends JPanel implements ActionListener{
		
		
		
		private double xVal;
		private double yVal;
		private JLabel labelX, labelY, labelResult;
		private JTextField fieldX, fieldY;
		private JButton buttPlus, buttMinus, buttMultuply, buttDivide;
		private JPanel panelX, panelY, panelButtons;
				
		public MainPanel (){
			
			setBackground(Color.gray);
			
			 setBorder( BorderFactory.createEmptyBorder(5,5,5,5) );
			
			panelX = new JPanel();
			labelX = new JLabel(" x = ");
			fieldX = new JTextField("0", 10);
			panelX.add(labelX);
			panelX.add(fieldX);
			
			panelY = new JPanel();
			labelY = new JLabel(" y = ");
			fieldY = new JTextField("0", 10);
			panelY.add(labelY);
			panelY.add(fieldY);
			
			panelButtons = new JPanel();
			panelButtons.setLayout(new GridLayout(1,4));
			buttDivide = new JButton("/");
			buttDivide.addActionListener(this);
			
			buttMinus = new JButton("-");
			buttMinus.addActionListener(this);
			
			buttMultuply = new JButton("*");
			buttMultuply.addActionListener(this);
			
			buttPlus = new JButton("+");
			buttPlus.addActionListener(this);
			
			panelButtons.add(buttDivide);
			panelButtons.add(buttMinus);
			panelButtons.add(buttMultuply);
			panelButtons.add(buttPlus);
			
			labelResult = new JLabel("result", JLabel.CENTER);
			labelResult.setOpaque(true);
		
			setLayout(new GridLayout(4,1,3,3));
			
			add(panelX);
			add(panelY);
			add(panelButtons);
			add(labelResult);		
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			try{
				String str = fieldX.getText();
				xVal = Double.parseDouble(str);
			}
			catch (NumberFormatException n) {
			      labelResult.setText("Illegal data for x.");
			      fieldX.requestFocus();
			      return;
			}
			try{
				String str = fieldY.getText();
				yVal = Double.parseDouble(str);
			}
			catch (NumberFormatException n) {
			      labelResult.setText("Illegal data for x.");
			      fieldY.requestFocus();
			      return;
			}
			if (e.getSource()==buttDivide){
				 labelResult.setText("X / Y = " + (xVal/yVal));
			}
			else if (e.getSource()==buttMinus){
				labelResult.setText("X - Y = " + (xVal-yVal));
			}
			else if (e.getSource()==buttMultuply){
				labelResult.setText("X * Y = " + (xVal*yVal));
			}
			else if (e.getSource()==buttPlus){
				labelResult.setText("X + Y = " + (xVal+yVal));
			}
			
		}
		
	} //end of class MainPanel
	

		   
}// end of class FourActionsCalc
